package com.mindmap.util;

import com.mindmap.model.MindMap;
import com.mindmap.model.Node;
import java.awt.*;

public class LayoutManager {
    private static final int NODE_WIDTH = 160;  // 增加节点宽度
    private static final int NODE_HEIGHT = 50;
    private static final int HORIZONTAL_GAP = 100;  // 增加水平间距
    private static final int VERTICAL_GAP = 60;    // 增加垂直间距
    private static final int LEVEL_GAP = 150;      // 增加层级间距

    public static void autoLayout(MindMap mindMap) {
        if (mindMap == null || mindMap.getRoot() == null) return;

        Node root = mindMap.getRoot();
        Dimension panelSize = mindMap.getPanelSize();
        root.setBounds(new Rectangle(
            panelSize.width/2 - NODE_WIDTH/2, 
            panelSize.height/2 - NODE_HEIGHT/2, 
            NODE_WIDTH, 
            NODE_HEIGHT
        ));

        int childCount = root.getChildren().size();
        if (childCount == 0) return;

        // 计算左右分布比例
        int leftCount = (int) Math.ceil(childCount / 2.0);
        int rightCount = childCount - leftCount;

        // 左侧子节点
        layoutChildren(root, 0, leftCount, true, 1);

        // 右侧子节点
        layoutChildren(root, leftCount, childCount, false, 1);
    }

    private static void layoutChildren(Node parent, int start, int end, boolean left, int level) {
        int childCount = end - start;
        if (childCount == 0) return;

        Rectangle parentBounds = parent.getBounds();
        int parentX = parentBounds.x;
        int parentY = parentBounds.y;
        int parentWidth = parentBounds.width;
        int parentHeight = parentBounds.height;

        // 根据层级调整间距
        int levelGap = LEVEL_GAP - (level * 15);
        if (levelGap < 80) levelGap = 80;

        // 计算子节点总高度
        int totalHeight = childCount * NODE_HEIGHT + (childCount - 1) * VERTICAL_GAP;
        int startY = parentY + parentHeight / 2 - totalHeight / 2;

        // 计算子节点X坐标
        int x = left ? parentX - levelGap - NODE_WIDTH : parentX + parentWidth + levelGap;

        for (int i = start; i < end; i++) {
            Node child = parent.getChildren().get(i);
            child.setBounds(new Rectangle(x, startY, NODE_WIDTH, NODE_HEIGHT));
            startY += NODE_HEIGHT + VERTICAL_GAP;

            // 递归布局子节点
            if (!child.getChildren().isEmpty()) {
                layoutChildren(child, 0, child.getChildren().size(), left, level + 1);
            }
        }
    }

    public static void leftLayout(MindMap mindMap) {
        if (mindMap == null || mindMap.getRoot() == null) return;

        Node root = mindMap.getRoot();
        Dimension panelSize = mindMap.getPanelSize();
        root.setBounds(new Rectangle(
            panelSize.width - LEVEL_GAP - NODE_WIDTH, 
            panelSize.height/2 - NODE_HEIGHT/2, 
            NODE_WIDTH, 
            NODE_HEIGHT
        ));
        layoutChildren(root, 0, root.getChildren().size(), true, 1);
    }

    public static void rightLayout(MindMap mindMap) {
        if (mindMap == null || mindMap.getRoot() == null) return;

        Node root = mindMap.getRoot();
        Dimension panelSize = mindMap.getPanelSize();
        root.setBounds(new Rectangle(
            LEVEL_GAP, 
            panelSize.height/2 - NODE_HEIGHT/2, 
            NODE_WIDTH, 
            NODE_HEIGHT
        ));
        layoutChildren(root, 0, root.getChildren().size(), false, 1);
    }

   
}